package com.edu.controller;

import com.edu.dao.MyBatisDaoImpl;
import com.edu.data.Contact;
import com.edu.data.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/contact")
public class ContactController {
    @Autowired
    MyBatisDaoImpl dao;
    //    /contact/list
    @RequestMapping({"/list"})
    public String listContact(Model model, @RequestParam(value = "start",required = false,defaultValue = "0")  String start){
        int startNum=0;
        if(start!=null&&start.length()>0){
            startNum=Integer.parseInt(start);
        }
        long total=dao.getContactsCount();
        Page page=new Page((int) total,startNum,5);
        List<Contact> contactList=dao.getAllContactsByPage(page);
        model.addAttribute("contactList",contactList);
        model.addAttribute("page",page);

        System.out.println("ContactController打印");
        return "main";
    }
    @RequestMapping({"/admin"})
    public String listContactToMain(Model model, @RequestParam(value = "start",required = false,defaultValue = "0")  String start){
        int startNum=0;
        if(start!=null&&start.length()>0){
            startNum=Integer.parseInt(start);
        }
        long total=dao.getContactsCount();
        Page page=new Page((int) total,startNum,5);
        List<Contact> contactList=dao.getAllContactsByPage(page);
        model.addAttribute("contactList",contactList);
        model.addAttribute("page",page);
        return "admin";
    }
    // /contact/add
    @RequestMapping("/toAdd")
    public String toAddContact(){
        return "register";    //web-inf/views/register.jsp
    }
    @RequestMapping("/add")
    public String addContact(Contact contact) throws ServletException, IOException {
        dao.addContact(contact);
        return "forward:/contact/list";
    }
    @RequestMapping("/edit")
    public String editContact(Contact contact,HttpServletRequest request){
        dao.updateContact(contact);
        //request.getRequestDispatcher().forward();
        return "forward:/contact/admin";//
    }
    @RequestMapping("/toEdit")
    public String toEditContact(@RequestParam("id")String id,Model model){
        int idNum=0;
        if(id!=null&&id.length()>0){
            idNum=Integer.parseInt(id);
        }
        Contact contact=dao.getContact(idNum);
        model.addAttribute("contact",contact);
        return "edit";
    }
    @RequestMapping("/delete")
    public String deleteContact(@RequestParam("id")String id){
        int idNum=0;
        if(id!=null&&id.length()>0){
            idNum=Integer.parseInt(id);
        }
        dao.deleteContact(idNum);
        return "forward:/contact/admin";
    }
}
